<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="追溯工位:" prop="ulocId">
            <el-select v-model="formModel.tmBasUlocId" clearable filterable>
              <el-option
                v-for="item in ulocOptions"
                :key="item.tmBasUlocId"
                :label="item.ulocNo+'-'+item.ulocName"
                :value="item.tmBasUlocId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="整车物料编码:" prop="materialDataId">
            <el-select v-model="formModel.materialDataId" clearable filterable>
              <el-option
                v-for="item in carMaterialDataOptions"
                :key="item.materialDataId"
                :label="item.materialNo+'-'+item.materialDesc"
                :value="item.materialDataId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="零件追溯件代码:" prop="partTrackNoId">
            <el-select v-model="formModel.partTrackNoId" clearable filterable @change="changePartTrackNo">
              <el-option
                v-for="item in partTrackNoOptions"
                :key="item.partTrackNoId"
                :label="item.partTrackNo+'-'+item.partTrackNoName"
                :value="item.partTrackNoId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="零件物料编码:" prop="partTrackGroupNumId">
            <el-select v-model="formModel.partTrackGroupNumId" clearable filterable>
              <el-option
                v-for="item in partMaterialDataOptions"
                :key="item.partTrackGroupNumId"
                :label="item.materialNo+'-'+item.materialDesc"
                :value="item.partTrackGroupNumId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="对称件标记:" prop="symmetricalPartTab">
            <hey-select v-model="formModel.symmetricalPartTab" clcd="ind_cd" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="前中后标记:" prop="locationTab">
            <hey-select v-model="formModel.locationTab" clcd="location_tab" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态:" prop="status">
            <hey-select v-model="formModel.status" clcd="is_effective_cd" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'PartTrackConfigMnt',
  props: ['pageType', 'editData'],
  data() {
    return {
      tableData: [],
      ulocOptions: [],
      carMaterialDataOptions: [],
      partTrackNoOptions: [],
      partMaterialDataOptions: [],
      formModel: {
        partTrackConfigId: '',
        tmBasUlocId: '',
        materialDataId: '',
        partTrackNoId: '',
        partTrackGroupNumId: '',
        symmetricalPartTab: '0',
        locationTab: '',
        status: '1'
      },
      // 老数据
      oldFormModel: {
      },
      rules: {}
    }
  },
  computed: {},
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.$form = this.$refs.form
      const { pageType, editData, formModel } = this
      const param = { status: '1' }
      if (pageType !== 'add') {
        this.assignData(editData, formModel)
        this.oldFormModel = JSON.parse(JSON.stringify(this.formModel))
        this.getUlocList(param)
        const carParam = { materialTypeKey: 'car', status: '1' }
        this.getCarMaterialDataOptions(carParam)
        this.getPartTrackNoOptions(param)
        if (this.formModel.partTrackNoId !== '') {
          const partParam = { status: '1', partTrackNoId: this.formModel.partTrackNoId }
          this.getPartMaterialDataOptions(partParam)
        }
      } else {
        this.getUlocList(param)
        const carParam = { materialTypeKey: 'car', status: '1' }
        this.getCarMaterialDataOptions(carParam)
        this.getPartTrackNoOptions(param)
      }
      this.initRules()
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        tmBasUlocId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        materialDataId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        partTrackNoId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        partTrackGroupNumId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        symmetricalPartTab: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        status: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }]
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    /** 查询追溯工位列表 */
    getUlocList(param) {
      this.get('/tmBasUloc/queryTmBasUloc', param).then(res => {
        this.ulocOptions = res.list
      })
    },
    /** 查询整车物料编码列表 */
    getCarMaterialDataOptions(param) {
      this.get('/materialData/listMaterialData', param).then(res => {
        this.carMaterialDataOptions = res
      })
    },
    /** 查询零件追溯件代码 */
    getPartTrackNoOptions(param) {
      this.get('/partTrackNo/listPartTrackNo', param).then(res => {
        this.partTrackNoOptions = res
      })
    },
    /** 查询零件物料编码列表 */
    getPartMaterialDataOptions(param) {
      this.get('/partTrackGroupNum/listPartTrackGroupNum', param).then(res => {
        this.partMaterialDataOptions = res
      })
    },
    /** 点击后根据选择的精确追溯件代码ID查询对应的零件物料编码列表 */
    changePartTrackNo(value) {
      // 开启零件物料编码选择
      const param = { partTrackNoId: value, status: '1' }
      this.formModel.partTrackGroupNumId = ''
      this.get('/partTrackGroupNum/listPartTrackGroupNum', param).then(res => {
        this.partMaterialDataOptions = res
      })
    },
    /** 修改新增提交 */
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.formModel.partTrackConfigId !== '') {
            // 判断是否修改
            if (JSON.stringify(this.formModel) !== JSON.stringify(this.oldFormModel)) {
              const tip = '修改成功！'
              const url = '/partTrackConfig/update'
              this.post(url, this.formModel).then(res => {
                this.$info(tip, () => {
                  this.$emit('mntSuc', res)
                  this.closeDialog()
                })
              })
            } else {
              this.$emit('mntSuc')
              this.closeDialog()
            }
          } else {
            const url = '/partTrackConfig/save'
            this.post(url, this.formModel).then(res => {
              const tip = '新增成功！'
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          }
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
